package com.enjoy.concurrent.ch8;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Random;

/**
 * 查询任务
 */
@Slf4j
public class AppTest {

    private static final String JOB_NAME = "计算数值";

    private static final int JOB_LENGTH = 1000;

    private static class QueryResult implements Runnable {

        private PendingJobPool pool;

        public QueryResult(PendingJobPool pool) {
            this.pool = pool;
        }

        @Override
        public void run() {
            int i = 0;

            while (i <= 350) {
                List<?> taskDetail = pool.getTaskDetail(JOB_NAME);
                if (taskDetail != null) {
                    log.info(taskDetail.toString());
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
            }
        }
    }

    public static void main(String[] args) {

        MyTask myTask = new MyTask();
        PendingJobPool pool = PendingJobPool.getInstance();
        pool.registerJob(JOB_NAME,JOB_LENGTH,myTask,5000);
        Random random = new Random();
        for (int i = 0; i < JOB_LENGTH; i++) {
            pool.putTask(JOB_NAME, random.nextInt(1000));
        }
        new Thread(new QueryResult(pool)).start();
    }
}
